package com.wetao.module_report;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.wetao.module_report.fragment.ReportSingleFragment;
import com.wetao.module_report.fragment.ReportStageFragment;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.google.gson.reflect.TypeToken;
import com.wetao.lib_common.ConstantArouter;
import com.wetao.lib_common.Constants;
import com.wetao.lib_common.EventBus.EventBusMessage;
import com.wetao.lib_common.base.BaseActivity;
import com.wetao.lib_common.bean.TabEntity;
import com.wetao.lib_common.bean.response.GetSubjectList;
import com.wetao.lib_common.contract.EmptyCon;
import com.wetao.lib_common.presenter.EmptyPre;
import com.wetao.lib_common.tsdBlePen.TsdService;
import com.wetao.lib_common.utils.JsonUtils;
import com.wetao.lib_common.utils.LogUtils;
import com.wetao.lib_common.utils.SPUtils;
import com.wetao.lib_common.utils.ToastUtil;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

@Route(path = ConstantArouter.PATH_REPORT_MAINACTIVITY)
public class ReportMainActivity extends BaseActivity<EmptyCon.View, EmptyPre>
        implements EmptyCon.View, ReportSingleFragment.OnFragmentInteractionListener, ReportStageFragment.OnFragmentInteractionListener {

    private static final int HANDLER_INIT_VP_TAB = 1;
    public static final int REQUEST_LINE_CODE = 100;
    private ViewPager viewPager;
    private CommonTabLayout commonTabLayout;
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    private FragmentManager fragmentManager;//声明fragment管理
    private List<Fragment> fragmentList = new ArrayList<>();
    private String[] mTitles = {"单次学情", "阶段学情"};
    private ImageView iv_close;
    private ReportSingleFragment singleFragment;
    private ReportStageFragment stageFragment;

    private MyHandler handler;

    static class MyHandler extends Handler {
        WeakReference<ReportMainActivity> weakReference;
        public MyHandler(ReportMainActivity activity) {
            weakReference = new WeakReference(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            if(weakReference.get() == null) {
                return;
            }
            ReportMainActivity activity = weakReference.get();
            if(msg.what == HANDLER_INIT_VP_TAB) {
                activity.initVpTab();
            }
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.report_main_activity;
    }

    @Override
    public boolean getIsBindTsdService() {
        return false;
    }

    @Override
    public TsdService.OnDataReceiveListener getTsdServiceDataReceiveListener() {
        return null;
    }

    @Override
    public EmptyPre createPresenter() {
        return new EmptyPre(this);
    }

    @Override
    public EmptyCon.View createView() {
        return this;
    }

    @Override
    public void init() {
        handler = new MyHandler(this);

        iv_close = findViewById(R.id.iv_close);
        iv_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        viewPager = findViewById(R.id.vp);

        commonTabLayout = findViewById(R.id.tl_2);

        handler.sendEmptyMessageDelayed(HANDLER_INIT_VP_TAB, 100);

        String str = SPUtils.getInstance().getString(Constants.GET_SUBJECT_LIST);
        List<GetSubjectList> list = JsonUtils.fromJsonList(str, new TypeToken<List<GetSubjectList>>() {}.getType());
        if(list == null || list.size() == 0) {
            ToastUtil.initToast("请重新登录获取学科");
        }

    }

    @Override
    protected void onDestroy() {
        if(handler != null) {
            handler.removeCallbacksAndMessages(null);
            handler = null;
        }
        super.onDestroy();
    }

    public void initVpTab(){

        singleFragment = ReportSingleFragment.newInstance();
        stageFragment = ReportStageFragment.newInstance();
        fragmentList.add(singleFragment);
        fragmentList.add(stageFragment);

        fragmentManager = getSupportFragmentManager();

        FragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(fragmentManager);
        viewPager.setAdapter(pagerAdapter);
        viewPager.setOffscreenPageLimit(fragmentList.size());

        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i], 0, 0));
        }

        commonTabLayout.setTabData(mTabEntities);
        commonTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                viewPager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });

    }

    class MyFragmentPagerAdapter extends FragmentPagerAdapter {
        public MyFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
        }
        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return super.getPageTitle(position);
        }
    }
}
